<?php
class CertificateInfomation{
	private $UniqueID = array();
	private $SecretCode = array();
	private $Infomation = array();
	function __construct($uniqueid){
		$this->UniqueID = unpack('C*', $uniqueid ) ;
		$this->SecretCode = unpack('C*', UtilHash::generateKey( 32 )  ) ;
	}
	function getUniqueID(){
		return implode(array_map("chr",  $this->UniqueID )) ;
	}
	function getSecretCode(){
		return implode(array_map("chr",  $this->SecretCode )) ;
	}
	function setInfomation($infomation){
		$this->Infomation = unpack('C*', $infomation ) ;
	}
	function getInfomation(){
		return implode(array_map("chr",  $this->Infomation )) ;
	}
	static function swap($bytebuffer){
		$uniqueid = implode(array_map("chr",  DataUtil::readBBin( $bytebuffer )  )) ;
		$obj = new CertificateInfomation($uniqueid);
		$obj->SecretCode = DataUtil::readBBin( $bytebuffer ) ;
		$obj->Infomation = DataUtil::readSBin( $bytebuffer ) ;
		return $obj;
	}
	function getBinSize(){
		$size = 0;
		$size += 1  + count($this->UniqueID);
		$size += 1  + count($this->SecretCode);
		$size += 2  + count($this->Infomation);
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeBBin( $buf , $this->UniqueID ) ;
		DataUtil::writeBBin( $buf , $this->SecretCode ) ;
		DataUtil::writeSBin( $buf , $this->Infomation ) ;
		return  $buf ->_array() ;
	}
}
class DataUserCertificates{
	private $CertificatesList = array();
	function __construct(){
	}
	static function swap($bytebuffer){
		$obj = new DataUserCertificates();
		$record = DataUtil::readInt32( $bytebuffer ) ;
		for($i = 0; $i < $record; $i++){
			$cer = CertificateInfomation::swap($bytebuffer);
			$obj->CertificatesList[] = $cer;
		}
		return $obj;
	}
	function createCertificate($uniqueid,$infomation){
		if(is_array($this->CertificatesList)){
			foreach($this->CertificatesList as $cer){
				if($cer->getUniqueID() == $uniqueid){
					return $cer->getSecretCode();
				}
			}
		}
		$cer = new CertificateInfomation($uniqueid);
		$cer->setInfomation($infomation);
		$this->CertificatesList[] = $cer;
		return $cer->getSecretCode();
	}
	function getCertificate($uniqueid){
		if(is_array($this->CertificatesList)){
			foreach($this->CertificatesList as $cer){
				if($cer->getUniqueID() == $uniqueid){
					return $cer;
				}
			}
		}
		return null;
	}
	function getCertificates(){
		return $this->CertificatesList;
	}
	function removeCertificate($uniqueid){
		if(is_array($this->CertificatesList)){
			foreach($this->CertificatesList as $key=>$cer){
				if($cer->getUniqueID() == $uniqueid){
					unset($this->CertificatesList[$key]);
					$this->CertificatesList = array_values($this->CertificatesList);
					return true;
				}else{
				}
			}
		}
		return false;
	}
	function getBinSize(){
		$size = 0;
		$size += 4 ;
		if(is_array($this->CertificatesList)){
			foreach($this->CertificatesList as $cer){
				$size += $cer->getBinSize();
			}
		}
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf , count($this->CertificatesList) ) ;
		if(is_array($this->CertificatesList)){
			foreach($this->CertificatesList as $cer){
				DataUtil::writeBytes( $buf , $cer->getBinary() ) ;
			}
		}
		return  $buf ->_array() ;
	}
}
?>